import SafeCall from "./SafeCall.js";

/**
 *
 * @param onClick {Function}
 * @param onLongClick {Function}
 */
export default function longClickUtil(onClick, onLongClick) {
    let longClickTimeout = 1000
    let longClicked = false
    let longClickTimer = -1

    const onLeave = () => {
        clearTimeout(longClickTimer)
        longClicked = false
    }

    const onDown = () => {
        longClickTimer = setTimeout(() => {
            SafeCall(onLongClick)
            longClicked = true
        }, longClickTimeout)
    }

    const onUp = () => {
        if (!longClicked) {
            SafeCall(onClick)
        }
        onLeave()
    }
    return {onUp, onDown, onLeave}
}